package com.yy.permissions_general.module.system.menu.controller;

import com.yy.permissions_general.module.system.aopLog.aop.MyLog;
import com.yy.permissions_general.module.system.menu.entity.Auth;
import com.yy.permissions_general.module.system.menu.service.AuthService;
import com.yy.permissions_general.utils.ResultUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * <p>
 * 权限前端控制器
 * </p>
 *
 * @author YY
 * @since 2021-08-07
 */
@RestController
@RequestMapping("/auth")
public class AuthController {

    @Resource
    private AuthService authService;

    /**
    * 查询列表
    * @param auth
    * @param limit
    * @param page
    * @return
    */
    @RequestMapping("/list")
    @MyLog("查询菜单")
    public ResultUtil list(Auth auth, @RequestParam(value = "limit",defaultValue = "999999")int limit, @RequestParam(value = "limit",defaultValue = "0")int page) {
      return ResultUtil.buildPagination(authService.selectPageList(auth,limit,page));
    }

    /**
    * 新增数据
    * @param auth
    * @return
    */
    @RequestMapping("/addAuth")
    @MyLog("添加菜单")
    @PreAuthorize("hasPermission('/auth/addAuth','auth:addAuth')")
    public ResultUtil addAuth(Auth auth) {
        authService.addAuth(auth);
        return ResultUtil.success();
    }

    /**
    * 修改数据
    * @param auth
    * @return
    */
    @RequestMapping("/editAuth")
    @MyLog("修改菜单")
    @PreAuthorize("hasPermission('/auth/editAuth','auth:editAuth')")
    public ResultUtil editAuth(Auth auth) {
        authService.editAuth(auth);
        return ResultUtil.success();
    }

    /**
    * 查看详情
    * @param id
    * @return
    */
    @RequestMapping("/detail")
    @MyLog("查询菜单信息")
    public ResultUtil detail(Integer id) {
        Auth auth = authService.getDetailsById(id);
        return ResultUtil.success(auth);
    }

    /**
    * 根据ID删除记录
    * @param id
    * @return
    */
    @RequestMapping("/delete")
    @MyLog("删除菜单")
    @PreAuthorize("hasPermission('/auth/delete','auth:delete')")
    public ResultUtil delete(Integer id) {
        authService.deleteById(id);
        return ResultUtil.success();
    }

    /**
     * @MethodName: selectAuthByUser
     * @Description: TODO 首页初始化菜单
     * @Param: []
     * @Return: com.yy.permissions_general.utils.ResultUtil
     * @Author: YangYI
     * @Date: 2021/11/28
    **/
    @RequestMapping("/selectAuthByUser")
    @MyLog("首页初始化菜单")
    public ResultUtil selectAuthByUser() {
        return ResultUtil.success(authService.selectAuthByUser());
    }

}
